import {observable, action, toJS} from 'mobx';
import { message, Button } from 'antd';
import { WeaForm, WeaTableNew } from 'comsMobx';
import {WeaLocaleProvider} from 'ecCom';
import * as goalBase from '../../apis/goal/goalBase'
const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export default class GoalBaseStore{

    @observable loading = true; // 页面初始化的loading状态：数据加载成功前后前使用

    //导入
    @observable reslutData = {}  //导入的结果
    @observable ifupload = true;

    //导出
    @observable tableStore = new TableStore(); // new table
    @observable hasRight = false;
    @observable spinloading = false;
    @observable radioGroupParams={};
    @observable conditionInfo=[];

    doInit = () =>{
        this.goalExportCondition({});
    }

    //目标导入
    @action
    goalPersonImportInfo = (params) => {
        this.loading = true;
        this.ifupload = false;
        goalBase.goalPersonImportInfo(params).then(action(res=>{
            if(res.status){
                if(res.result == "success"){
                    message.success(getLabel(129463,'导入成功'));
                }else{
                    message.error(getLabel('25009','失败'));
                }
                this.reslutData = res; 
                this.loading = false;
                this.ifupload = true;
                if(typeof params.callback === "function"){
                    params.callback();
                }
            }
        }));
    }

    @action("获取设置条件")
    goalExportCondition = (params) =>{
        params.type = "get";
        this.loading = true;
        goalBase.goalExport(params).then(action(res=>{
            if(res.status){
                this.hasRight = res.hasRight;
                this.conditionInfo = res.conditioninfo;
                this.radioGroupParams = toJS(res.radioGroupParams);
                this.goaltableList(res.radioGroupParams);
            }
            this.loading = false;
        }));
    }

    @action("获取目标导出列表")
    goaltableList = (params) =>{
        this.loading = true;
        params.exporttype = 1;
        goalBase.goaltableList(params).then(action(res => {
            if (res.status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    }

    @action("查询条件变化")
    updateRadioGroupParams = (params) => {
        if(typeof(params.userRange)!="undefined" && params.userRange!=toJS(this.radioGroupParams.userRange)){
          this.radioGroupParams.createrid='';
          this.radioGroupParams.principalid='';
          this.radioGroupParams.partnerid='';
          //this.conditionInfo[1].selectLinkageDatas[2].browserConditionParam.replaceDatas = []
        }
        for(let key  in params){
          this.radioGroupParams[key] = params[key];
        }
        let radioGroupParams = this.radioGroupParams;
        this.goaltableList(radioGroupParams);
    };


    //目标导出
    @action
    goalExport = (params) => {
        let dataKey = this.tableStore.dataKey;
        this.loading = true;
        goalBase.getExportColumn({dataKey:dataKey}).then(result => {
            let columns = "";
            let sData = toJS(result.destdatas);
            sData.map(item=>{
                columns+=item.dbField+",";
            });
            if(columns==""){
                message.error(getLabel('508818', '接口调用失败！'))
            }else{
                let params = this.radioGroupParams;
                params.columns = columns;
                goalBase.goalExport(params).then(data => {
                    if (data.url) {
                        window.location.href = `${location.origin}${data.url}`
                    }
                });
            }
            this.loading = false;
        });
    }



    sleep = (time)=> {
        return new Promise((resolve) => setTimeout(resolve, time));
    }
  
}